<template>
	<view>
		<navbar name="权限设置"></navbar>
		<view class="w750bgf">
			<view class="jurisdiction-v">
				<view class="jurisdiction-t">
					允许健康顾问推送商品给我
				</view>
				<switch color="#2EA7E0" checked @change="switch1Change" :checked="pushStatus==1?true:false" />
			</view>
			<u-line />
			<view class="jurisdiction-v">
				<view class="jurisdiction-t">
					允许健康顾问查看我的健康报告
				</view>
				<switch color="#2EA7E0" checked @change="switch2Change" :checked="viewStatus==1?true:false" />
			</view>
		</view>
		<button class="quit" type="default" @tap="updatePermission(pushStatus,viewStatus)">保存</button>
	</view>
</template>

<script>

import uLine from "@/uview-ui/components/u-line/u-line.vue"

	export default {
		components: {
			uLine,
		},
		data() {
			return {
				pushStatus:0,
				viewStatus:0,
			}
		},
		onLoad() {
			this.getPermission()
		},
		methods: {
			updatePermission(pushStatus,viewStatus){
				let _this = this;
				let fdata = {
					pushStatus:pushStatus,
					viewStatus:viewStatus,
					userId:uni.getStorageSync(global.IS_DEV?'USER_DATA_BSY_T':'USER_DATA_BSY_Z').userId
				}
				this.$loginl.put(global.apiUrls.updatePermission, fdata)
					.then(res => {
						if (res.data.code == 1000) {
							uni.showToast({
								title: res.data.message,
								duration: 2000
							});
						}else{
							uni.showToast({
								title: res.data.message,
								icon:'none',
								duration: 2000
							});
						}
					}).catch(err => {})
			},
			switch1Change(e) {
				let value = e.detail.value;
				this.pushStatus = value?1:0;
			},
			switch2Change(e) {
				let value = e.detail.value;
				this.viewStatus = value?1:0;
			},
			getPermission() {
				let _this = this;
				this.$loginl.get(global.apiUrls.getPermission + '?userId=' + uni.getStorageSync(global.IS_DEV?'USER_DATA_BSY_T':'USER_DATA_BSY_Z').userId, {})
					.then(res => {
						if (res.data.code == 1000) {
							let result = res.data.result;
							_this.pushStatus = result.pushStatus;
							_this.viewStatus = result.viewStatus;
						}
					}).catch(err => {})
			}
		}
	}
</script>

<style lang="scss">
	@import "./base.scss"
</style>
